package com.bibrApp.difu.actvity.jiechu_mvp;


import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bibrApp.difu.R;
import com.bibrApp.difu.Response.C2CJiedaiResBody;
import com.bibrApp.difu.Response.FBInfoResBody;
import com.bibrApp.difu.base.BaseActivity;
import com.bibrApp.difu.dialog.CustomDialog;
import com.bibrApp.difu.utils.BigdecimalUtils;
import com.bibrApp.difu.utils.CustomDialogUtils;
import com.bibrApp.difu.utils.StrValidateUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.bibrApp.difu.utils.UIUtils;
import com.bibrApp.difu.widget.DFDialog;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.OnClick;

public class JiechuActivity extends BaseActivity<JiechuPresenter> implements IJiechuView, TextWatcher {
    private Context mContext;
    private C2CJiedaiResBody.JiedaiBean jiedaiBean;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_jiechu_bg)
    ImageView iv_jiechu_bg;
    @BindView(R.id.et_price)
    EditText et_price;//btc不可输入金额，eth可输入
    @BindView(R.id.tv_high_price)
    TextView tv_high_price;//最高价
    @BindView(R.id.tv_zrate)
    TextView tv_zrate;//质押率
    @BindView(R.id.tv_yrate)
    TextView tv_yrate;//年化
    @BindView(R.id.tv_drate)
    TextView tv_drate;//日化
    @BindView(R.id.tv_jdzq)
    TextView tv_jdzq;//周期
    @BindView(R.id.tv_lx)
    TextView tv_lx;//到期利息
    @BindView(R.id.tv_bx)
    TextView tv_bx;//本息
    @BindView(R.id.bt_commit)
    Button bt_commit;

    @BindView(R.id.tv_xieyi)
    TextView tvXieyi;
    @BindView(R.id.tv_rule)
    TextView tvRule;
    private boolean isXieyiCheck;
    private String coinId;
    @Override
    protected void initPresenter() {
        presenter = new JiechuPresenter(this, this);
        presenter.init();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_jiechu;
    }


    @Override
    public void error(String msg) {

    }

    @Override
    public void success(String msg) {
        ToastUtils.ShowToast(mContext, msg);
        new DFDialog.Builder(this)
                .setTitle("恭喜您预借贷认购成功！")
                .setMessage("  当募集数量满后，我们将以短信的方式通知您，请您保持手机畅通和准备好相应数量的资金资产。认购后，放弃或者在规定时间内资金资产未到账，视为违约，将扣除保证金。请您注意")
                .setSure(true)
                .setPositiveButton("确定", null)
                .show();
    }

    @Override
    public void successOfGetFBInfo(FBInfoResBody fbInfoResBody) {
        presenter.querySingleAssets(fbInfoResBody.getId());
    }

    @Override
    public void initView() {
        mContext = this;
        tvRule.setText("您需要缴纳保证金才能发布");
        UIUtils.appendText(tvRule, "《保证金规则》", "#1D89E4");
        jiedaiBean = (C2CJiedaiResBody.JiedaiBean) getIntent().getSerializableExtra("bean");
        if (jiedaiBean == null) {
            return;
        }
        tv_title.setText("借出USDT给" + StrValidateUtils.replaceNameX(jiedaiBean.getNickname()));
        if (jiedaiBean.getPawnCoinEunit().equalsIgnoreCase("BTC")) {
            isEth = false;
            iv_jiechu_bg.setImageResource(R.mipmap.tab_btc);
            et_price.setText(jiedaiBean.getLegalQuantity());
            et_price.setEnabled(false);
            tv_high_price.setVisibility(View.GONE);
            tv_lx.setText(BigdecimalUtils.format2Str(Double.parseDouble(jiedaiBean.getTotalInterest()), 2));
            double bx = Double.parseDouble(jiedaiBean.getTotalInterest()) + Double.parseDouble(jiedaiBean.getLegalQuantity());
            tv_bx.setText(BigdecimalUtils.format2Str(bx, 2));
        } else {
            isEth = true;
            iv_jiechu_bg.setImageResource(R.mipmap.tab_eth);
            et_price.setHint("请输入金额");
            et_price.setEnabled(true);
            tv_high_price.setVisibility(View.VISIBLE);
            tv_high_price.setText("最高" + BigdecimalUtils.format2Str((Double.parseDouble(jiedaiBean.getHighPrice()) * 100), 0));
        }
        tv_zrate.setText("质押率:" + BigdecimalUtils.format2Str(Double.parseDouble(jiedaiBean.getPawnRate()) * 100, 0) + "%");
        tv_yrate.setText(BigdecimalUtils.format2Str((Double.parseDouble(jiedaiBean.getLegalDailyRate()) * 365 * 100), 2) + "%");
        tv_drate.setText(BigdecimalUtils.format2Str((Double.parseDouble(jiedaiBean.getLegalDailyRate()) * 100), 4) + "%");
        tv_jdzq.setText(jiedaiBean.getCycle() + "天");
        et_price.addTextChangedListener(this);
        presenter.getFBInfo();
    }

    @OnClick(R.id.tv_rule)
    public void ruleClick() {

    }

    @OnClick(R.id.bt_back)
    public void backClick() {
        finish();
    }

    @OnClick(R.id.tv_xieyi)
    public void xieyiClick() {
        isXieyiCheck = !isXieyiCheck;
        UIUtils.setDrawableLeft(tvXieyi, isXieyiCheck ? R.mipmap.icon_check : R.mipmap.icon_nocheck);
        typeChange();
    }

    @OnClick(R.id.bt_commit)
    public void commitClick() {
        CustomDialogUtils.showEditDialog(mContext, "请输入资金安全吗", "确认", new CustomDialog.OnEditClickListener() {
            @Override
            public void onPositiveClick(View view, String pwd) {
                String priceStr = et_price.getText().toString().replace(" ", "");
                if (!TextUtils.isEmpty(pwd) && pwd.length() >= 6) {
                    presenter.jieChuAd(jiedaiBean.getTallaId(), priceStr, pwd);
                } else {
                    ToastUtils.ShowToast(mContext, "资金安全码错误");
                }
            }
        });
    }

    /**
     * 发布借出按钮状态刷新
     */
    private boolean isEth;

    //需要勾选协议，必须缴纳保证金，必须输入金额
    private void typeChange() {
        if (isXieyiCheck && !TextUtils.isEmpty(et_price.getText().toString().replaceAll(" ", ""))) {
            bt_commit.setClickable(true);
            bt_commit.setEnabled(true);
        } else {
            bt_commit.setClickable(false);
            bt_commit.setEnabled(false);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        typeChange();
        calculate();
    }

    /**
     * 计算
     * 预计抵押=借贷金额/抵押率/实时币价
     */
    private void calculate() {
        String priceStr = et_price.getText().toString().replace(" ", "");
        if (TextUtils.isEmpty(priceStr)) {
            return;
        }
        BigDecimal priceBD = BigdecimalUtils.getBigDecimalPrice(priceStr, 0);
        BigDecimal dayRxBD = BigdecimalUtils.getBigDecimalPrice(jiedaiBean.getLegalDailyRate(), 5);
        BigDecimal dayLxBD = BigdecimalUtils.getBigDecimalPrice(String.valueOf(priceBD.multiply(new BigDecimal(jiedaiBean.getCycle())).multiply(dayRxBD)), 5);
        String lxStr = String.valueOf(BigdecimalUtils.getBigDecimalPrice(String.valueOf(priceBD.multiply(new BigDecimal(jiedaiBean.getCycle())).multiply(dayRxBD)), 5));
        tv_lx.setText(lxStr);
        tv_bx.setText(String.valueOf(priceBD.add(dayLxBD)));

    }
}
